/**
 * 
 */
package com.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.base.annotation.LoginNeedless;
import com.base.web.controller.BaseController;
import com.model.TextMessage;
import com.util.MessageUtil;

/**
 * @author ricky
 *
 */

@LoginNeedless
@Controller
public class WeixinMessageController extends BaseController{

	@RequestMapping(value="/message/weixin",method = RequestMethod.POST) 
	public @ResponseBody Object wexinMessage(HttpServletRequest request,HttpServletResponse response){
		try {
		
			
			
			JSONObject json  = MessageUtil.parseXml2JSON(request);
			if(json.getString("MsgType").equals("text")){
				String content = json.getString("Content");
				 // 发送方帐号（open_id）  
	            String fromUserName = json.getString("FromUserName");  
	            // 公众帐号  
	            String toUserName = json.getString("ToUserName");  
	            
	            TextMessage textMessage = new TextMessage();  
	            textMessage.setToUserName(fromUserName);  
	            textMessage.setFromUserName(toUserName);  
	            textMessage.setCreateTime(System.currentTimeMillis());  
	            textMessage.setMsgType("MsgType");  
	            textMessage.setFuncFlag(0);  
	            
				if("1".equals(content)){
					textMessage.setContent("你发送了1"); 
				}
				else if("2".equals(content)){
					textMessage.setContent("你发送了2"); 
				}else{
					textMessage.setContent("既不是1也不是2"); 
				}
				return textMessage;
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}
		return null;
	}
	
}
